今天是第二十七天我們可以寫一個javascript社交工程演練網頁程式管理系統,以下是我的程式碼
用戶管理:
演練設置:
數據分析:
通知管理:
以下是基本的框架,可以用來實現這個系統:
<!DOCTYPE html>
<html lang="zh-Hant">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>社交工程演練管理系統</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header>
<h1>社交工程演練管理系統</h1>
</header>
<main>
<div id="admin-login">
<h2>管理員登入</h2>
<form id="loginForm">
<label for="username">帳號:</label>
<input type="text" id="username" name="username" required>
<label for="password">密碼:</label>
<input type="password" id="password" name="password" required>
<button type="submit">登入</button>
</form>
</div>
<div id="dashboard" style="display: none;">
<h2>儀表板</h2>
<nav>
<ul>
<li><button id="manageUsersBtn">用戶管理</button></li>
<li><button id="setupDrillsBtn">演練設置</button></li>
<li><button id="viewReportsBtn">數據分析</button></li>
</ul>
</nav>
<section id="manageUsersSection" style="display: none;">
<h3>用戶管理</h3>
<button id="addUserBtn">新增用戶</button>
<div id="userList"></div>
</section>
<section id="setupDrillsSection" style="display: none;">
<h3>設置社交工程演練</h3>
<form id="drillForm">
<label for="drillName">演練名稱:</label>
<input type="text" id="drillName" name="drillName" required>
<label for="targetGroup">目標人群:</label>
<input type="text" id="targetGroup" name="targetGroup" required>
<label for="drillDescription">描述:</label>
<textarea id="drillDescription" name="drillDescription" required></textarea>
<button type="submit">建立演練</button>
</form>
<div id="drillList"></div>
</section>
<section id="viewReportsSection" style="display: none;">
<h3>數據分析</h3>
<div id="reports"></div>
</section>
</div>
</main>
<script src="app.js"></script>
</body>
</html>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
color: #333;
}
header {
background-color: #4CAF50;
color: white;
padding: 10px;
text-align: center;
}
h1, h2, h3 {
margin-bottom: 10px;
}
form {
display: flex;
flex-direction: column;
}
label {
margin-bottom: 5px;
}
input, textarea, button {
margin-bottom: 10px;
}
button {
padding: 10px;
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
section {
margin-top: 20px;
}
document.addEventListener("DOMContentLoaded", () => {
const loginForm = document.getElementById("loginForm");
const dashboard = document.getElementById("dashboard");
const manageUsersSection = document.getElementById("manageUsersSection");
const setupDrillsSection = document.getElementById("setupDrillsSection");
const viewReportsSection = document.getElementById("viewReportsSection");
loginForm.addEventListener("submit", (event) => {
event.preventDefault();
const username = document.getElementById("username").value;
const password = document.getElementById("password").value;
// 簡單驗證,實際應該從伺服器端進行驗證
if (username === "admin" && password === "password") {
document.getElementById("admin-login").style.display = "none";
dashboard.style.display = "block";
} else {
alert("帳號或密碼錯誤");
}
});
document.getElementById("manageUsersBtn").addEventListener("click", () => {
hideSections();
manageUsersSection.style.display = "block";
});
document.getElementById("setupDrillsBtn").addEventListener("click", () => {
hideSections();
setupDrillsSection.style.display = "block";
});
document.getElementById("viewReportsBtn").addEventListener("click", () => {
hideSections();
viewReportsSection.style.display = "block";
});
function hideSections() {
manageUsersSection.style.display = "none";
setupDrillsSection.style.display = "none";
viewReportsSection.style.display = "none";
}
// 簡單演練設置表單提交
const drillForm = document.getElementById("drillForm");
drillForm.addEventListener("submit", (event) => {
event.preventDefault();
const drillName = document.getElementById("drillName").value;
const targetGroup = document.getElementById("targetGroup").value;
const drillDescription = document.getElementById("drillDescription").value;
const drillList = document.getElementById("drillList");
const newDrill = document.createElement("div");
newDrill.textContent = `演練: ${drillName}, 目標: ${targetGroup}, 描述: ${drillDescription}`;
drillList.appendChild(newDrill);
drillForm.reset();
});
});
HTML 負責定義網頁的結構與內容。這部分的目的是建立網頁的基本框架,包括標題、表單和功能區域。
<!DOCTYPE html>
<html lang="zh-Hant">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>社交工程演練管理系統</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header>
<h1>社交工程演練管理系統</h1>
</header>
<main>
<div id="admin-login">
<h2>管理員登入</h2>
<form id="loginForm">
<label for="username">帳號:</label>
<input type="text" id="username" name="username" required>
<label for="password">密碼:</label>
<input type="password" id="password" name="password" required>
<button type="submit">登入</button>
</form>
</div>
<div id="dashboard" style="display: none;">
<h2>儀表板</h2>
<nav>
<ul>
<li><button id="manageUsersBtn">用戶管理</button></li>
<li><button id="setupDrillsBtn">演練設置</button></li>
<li><button id="viewReportsBtn">數據分析</button></li>
</ul>
</nav>
<section id="manageUsersSection" style="display: none;">
<h3>用戶管理</h3>
<button id="addUserBtn">新增用戶</button>
<div id="userList"></div>
</section>
<section id="setupDrillsSection" style="display: none;">
<h3>設置社交工程演練</h3>
<form id="drillForm">
<label for="drillName">演練名稱:</label>
<input type="text" id="drillName" name="drillName" required>
<label for="targetGroup">目標人群:</label>
<input type="text" id="targetGroup" name="targetGroup" required>
<label for="drillDescription">描述:</label>
<textarea id="drillDescription" name="drillDescription" required></textarea>
<button type="submit">建立演練</button>
</form>
<div id="drillList"></div>
</section>
<section id="viewReportsSection" style="display: none;">
<h3>數據分析</h3>
<div id="reports"></div>
</section>
</div>
</main>
<script src="app.js"></script>
</body>
</html>
<header>
:頁面的標題區塊,包含標題文字“社交工程演練管理系統”。<div id="admin-login">
):這是一個管理員登入表單,包含帳號和密碼輸入框,讓使用者能登入系統。<div id="dashboard">
):這個區域是登入後的主控頁面,隱藏起來,直到使用者成功登入才會顯示。包含用戶管理、演練設置和數據分析三個功能區。<section>
):分別代表用戶管理、設置演練和查看報告的區域。這些區域在初始頁面都隱藏,只有當使用者點擊相應的按鈕時才會顯示。CSS 用來美化網頁的外觀,設定元素的佈局和樣式。
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
color: #333;
}
header {
background-color: #4CAF50;
color: white;
padding: 10px;
text-align: center;
}
h1, h2, h3 {
margin-bottom: 10px;
}
form {
display: flex;
flex-direction: column;
}
label {
margin-bottom: 5px;
}
input, textarea, button {
margin-bottom: 10px;
}
button {
padding: 10px;
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
section {
margin-top: 20px;
}
body
:設定全頁面的字體、背景顏色及字體顏色。header
:設定標頭區域的樣式,包含背景顏色、字體顏色和內邊距,使標題區域顯得更醒目。form
、input
、button
:定義表單及按鈕的樣式,讓表單元件有一致的佈局與間距。button:hover
:這個 CSS 規則使當滑鼠移到按鈕上時,按鈕的背景顏色會改變,提供交互的視覺效果。JavaScript 負責處理前端的邏輯,例如表單提交、區域顯示/隱藏等互動操作。
document.addEventListener("DOMContentLoaded", () => {
const loginForm = document.getElementById("loginForm");
const dashboard = document.getElementById("dashboard");
const manageUsersSection = document.getElementById("manageUsersSection");
const setupDrillsSection = document.getElementById("setupDrillsSection");
const viewReportsSection = document.getElementById("viewReportsSection");
// 登入表單提交處理
loginForm.addEventListener("submit", (event) => {
event.preventDefault();
const username = document.getElementById("username").value;
const password = document.getElementById("password").value;
// 簡單驗證,這裡實際應從伺服器端進行驗證
if (username === "admin" && password === "password") {
document.getElementById("admin-login").style.display = "none";
dashboard.style.display = "block";
} else {
alert("帳號或密碼錯誤");
}
});
// 切換到用戶管理區
document.getElementById("manageUsersBtn").addEventListener("click", () => {
hideSections();
manageUsersSection.style.display = "block";
});
// 切換到演練設置區
document.getElementById("setupDrillsBtn").addEventListener("click", () => {
hideSections();
setupDrillsSection.style.display = "block";
});
// 切換到數據分析區
document.getElementById("viewReportsBtn").addEventListener("click", () => {
hideSections();
viewReportsSection.style.display = "block";
});
// 隱藏所有功能區
function hideSections() {
manageUsersSection.style.display = "none";
setupDrillsSection.style.display = "none";
viewReportsSection.style.display = "none";
}
// 處理演練設置表單提交
const drillForm = document.getElementById("drillForm");
drillForm.addEventListener("submit", (event) => {
event.preventDefault();
const drillName = document.getElementById("drillName").value;
const targetGroup = document.getElementById("targetGroup").value;
const drillDescription = document.getElementById("drillDescription").value;
const drillList = document.getElementById("drillList");
const newDrill = document.createElement("div");
newDrill.textContent = `演練: ${drillName}, 目標: ${targetGroup}, 描述: ${drillDescription}`;
drillList.appendChild(newDrill);
drillForm.reset(); // 清空表單
});
});
document.addEventListener("DOMContentLoaded")
:這段代碼確保當整個網頁載入完成後,才會執行內部的 JavaScript 代碼。loginForm.addEventListener
會捕捉事件,並防止頁面刷新(event.preventDefault()
)。碼被檢查(目前僅簡單比對 "admin"
和 "password"
),如果正確則隱藏登入表單並顯示儀表板。
hideSections()
函數:用來隱藏所有儀表板的子區域,這樣當使用者點擊按鈕時,只有點擊的區域會顯示,其他區域會隱藏。drillForm
提交處理:當設置演練表單提交時,會將演練名稱、目標人群及描述顯示在演練列表中,並重置表單。